package com.lingo.mgr.service.chatSession.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingo.common.core.domain.PageQuery;
import com.lingo.common.core.page.TableDataInfo;
import com.lingo.common.utils.StringUtils;
import com.lingo.mgr.domain.ChatSessionRecord;
import com.lingo.mgr.domain.bo.ChatSessionRecordBo;
import com.lingo.mgr.domain.vo.ChatSessionRecordVo;
import com.lingo.mgr.mapper.ChatSessionRecordMapper;
import com.lingo.mgr.service.chatSession.IChatSessionRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 对话记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-30
 */
@RequiredArgsConstructor
@Service
public class ChatSessionRecordServiceImpl implements IChatSessionRecordService {

    private final ChatSessionRecordMapper baseMapper;

    /**
     * 查询对话记录
     */
    @Override
    public ChatSessionRecordVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询对话记录列表
     */
    @Override
    public TableDataInfo<ChatSessionRecordVo> queryPageList(ChatSessionRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChatSessionRecord> lqw = buildQueryWrapper(bo);
        Page<ChatSessionRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询对话记录列表
     */
    @Override
    public List<ChatSessionRecordVo> queryList(ChatSessionRecordBo bo) {
        LambdaQueryWrapper<ChatSessionRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChatSessionRecord> buildQueryWrapper(ChatSessionRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChatSessionRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSessionId()), ChatSessionRecord::getSessionId, bo.getSessionId());
        lqw.eq(bo.getUserId() != null, ChatSessionRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageQuestion()), ChatSessionRecord::getMessageQuestion, bo.getMessageQuestion());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageAnswer()), ChatSessionRecord::getMessageAnswer, bo.getMessageAnswer());
        lqw.eq(bo.getAnswerResult() != null, ChatSessionRecord::getAnswerResult, bo.getAnswerResult());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateIp()), ChatSessionRecord::getCreateIp, bo.getCreateIp());
        return lqw;
    }

    /**
     * 新增对话记录
     */
    @Override
    public Boolean insertByBo(ChatSessionRecordBo bo) {
        ChatSessionRecord add = BeanUtil.toBean(bo, ChatSessionRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSessionRecordId(add.getSessionRecordId());
        }
        return flag;
    }

    /**
     * 修改对话记录
     */
    @Override
    public Boolean updateByBo(ChatSessionRecordBo bo) {
        ChatSessionRecord update = BeanUtil.toBean(bo, ChatSessionRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChatSessionRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除对话记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
